package com.jwdf.uda.view.prompt;

import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ViewPromptFindBinding;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.PromptUtil;
import com.jwdf.uda.view.dialog.BaseDialog;

/**
 * 类说明：
 */
public class FindPrompt extends BaseDialog implements DialogInterface.OnDismissListener {
    private ViewPromptFindBinding mBinding;
    private int deviceWidth;
    private boolean mCancelable;

    public FindPrompt(Context context) {
        super(context);
        this.deviceWidth = DisplayUtils.getDeviceWidth(context);
    }

    public FindPrompt(Context context,boolean cancelable) {
        this(context);
        mCancelable=cancelable;
    }

    private Handler handler = new Handler();
    private Runnable mFindRunnable = new Runnable() {
        @Override
        public void run() {
            int findPaddingLeft = deviceWidth / 2 - PromptUtil.getMeasuredWidth(mBinding.ivFind) / 2;
            int findTxtPaddingLeft = deviceWidth / 2 - PromptUtil.getMeasuredWidth(mBinding.ivFindTxt) / 2 - DisplayUtils.dip2px(getContext(), 15);
            mBinding.ivFind.setVisibility(View.VISIBLE);
            mBinding.ivFindTxt.setVisibility(View.VISIBLE);

            mBinding.ivFind.setPadding(findPaddingLeft, DisplayUtils.dip2px(getContext(), 10), 0, 0);
            mBinding.ivFindTxt.setPadding(findTxtPaddingLeft, DisplayUtils.dip2px(getContext(), 10), 0, 0);
        }
    };

    private Runnable mFindAddRunnable = new Runnable() {
        @Override
        public void run() {
            mBinding.ivFindAdd.setVisibility(View.VISIBLE);
            mBinding.ivFindAddTxt.setVisibility(View.VISIBLE);
            mBinding.ivFindAdd.setPadding(0, DisplayUtils.dip2px(getContext(), 10), DisplayUtils.dip2px(getContext(), 5), 0);
            mBinding.ivFindAddTxt.setPadding(0, DisplayUtils.dip2px(getContext(), 10), DisplayUtils.dip2px(getContext(), 5), 0);
        }
    };

    private Runnable mBtnRunnable = new Runnable() {
        @Override
        public void run() {
            mBinding.ivBtn.setVisibility(View.VISIBLE);
            mBinding.ivBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    };

    @Override
    protected View onInflateView(LayoutInflater inflater) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.view_prompt_find, null, false);
        mBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(View.VISIBLE==mBinding.ivBtn.getVisibility()){
                    dismiss();
                }
            }
        });
        return mBinding.getRoot();
    }

    @Override
    protected void onInitView(View view) {
        setOnDismissListener(this);

        //发现
        handler.postDelayed(mFindRunnable, 500);
        //发现添加
        handler.postDelayed(mFindAddRunnable, 1500);

        handler.postDelayed(mBtnRunnable, 3000);
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = DisplayUtils.getDeviceWidth(getContext());
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(mFindRunnable);
        handler.removeCallbacks(mFindAddRunnable);
        handler.removeCallbacks(mBtnRunnable);
        PromptUtil.setHideFindPrompt(getContext());
    }

    @Override
    public void onBackPressed() {
        if (mCancelable) {
            super.onBackPressed();
        }
    }
}
